<?php
/**
 * Created by PhpStorm.
 * User: hpp
 * Date: 2019/12/17
 * Time: 12:47
 */

namespace api\department\controller;


use api\common\model\RlEtCompanyModel;
use api\common\model\RlEtCustomerModel;
use api\common\model\RlHiseServiceProjectModel;
use cmf\controller\PublicController;
use api\common\service\FunctionService;
use api\common\model\RlCompanyModel;
use think\Db;
use think\Validate;

/**
 * 总部控制器
 * 总部登录
 * 工程：所有的工程列表
 * 巡检：根据电话+工程编号/工程id
 * Class HeadController
 * @package api\department\controller
 */
class HeadController extends PublicController
{
    protected $auth = [];
    // 部门类型
    protected $dept = [
        '营业部（总部部门）' => 3,
        '大区和课'  => 4,
        '分公司'   => 5,
    ];

    protected $status = [
        '进行中' => 1,
        '已完成' => 2,
        '已延期' => 3,
    ];

    /**
     * 安装单状态-new
     * @var array
     */
    protected $install_status_new = [
        0 => '未进场',
        1 => '已进场',
        2 => '隐蔽施工中',
        3 => '隐蔽施工已结束',
        4 => '调试',
        5 => '运行',
        'X' => '作废',
    ];

    public $dispatch =[
        '待接收'=>1,
        '已接受'=>2,
        '已拒收'=>3,
    ];

    protected $limit = 10;

    /**
     * 获取分公司、经销商、安装队
     *  @api http://xxxx.com/api/department/head/getcompany  get
     * @param type 1分公司 2经销商 3安装队
     * @param jxsbm 经销商编码 type=3的时候传
     * @param fgscode 分公司编码 type=2的时候传
     * type=1
     *      company_id      int     分公司主键id
     *      fgs             string  分公司编码
     *      fgsmc           string  分公司名称
     *      jxs_num         int     分公司下的经销商数量
     * type=2
     *      et_customer_id   int     经销商主键id
     *      jxsbm            string  经销商编码
     *      jxs_mc           string  经销商名称
     *      jxs_level        string  经销商等级
     *      azd_num          int     经销商下的安装队数
     * type=3
     *      et_azd_id        int     安装队主键id
     *      server_code      string  安装商代码
     *      engineer_code    string  工程师代码（安装队编码）
     *      engineer_name    string  工程师名称
     * {
     *      "code": 1,
     *      "msg": "ok",
     *      "data":
     *          {
     *              "et_azd_id": 1,
     *              "server_code": "AZ201412130081",
     *              "engineer_code": "AG65341",
     *              "engineer_name": "刘飞欣"
     *          }
     * }
     */
    public function getcompany(){
        $type = $this->request->param('type');
        if (!in_array($type, [1,2,3])) {
            $this->error('查询信息错误');
        }
        if ($type == 1){
            # 分公司
            $data = RlCompanyModel::getCompany();
        }elseif ($type == 2){
            # 经销商
            $fgscode = $this->request->param('fgscode');
            $data = RlCompanyModel::getJxs($fgscode);
        }else{
            # 安装队
            $jxsbm = $this->request->param('jxsbm');
            $data = RlCompanyModel::getAzd($jxsbm);
        }
        $this->success('请求成功',$data);
    }

    /**
     * 安装单列表
     * @api http://xxxx.com/api/department/head/installlist get
     * @param int    sfwc     2进行中 3已完成
     */
    public function installlist(){
        // 验证的规则
        $rule = [
//            'engineer_code' => 'require',
            'sfwc' => 'require',
        ];
        // 错误的消息
        $msg = [
//            'engineer_code.require' => '工程师代码不存在',
            'sfwc.require' => '安装单状态不存在',
        ];
        // 获取的数据
        $data = [
//            'engineer_code' => $this->request->get('engineer_code'), // 获取工程师代码
            'sfwc' => $this->request->get('sfwc/d', '2'), // 获取安装单状态
        ];
        // 直接传入验证规则
        $validate = Validate::make($rule)->message($msg);
        // 需要验证的数据
        $result = $validate->check($data);
        // 判断是否有验证错误
        if (!$result) {
            // 验证错误提示
            $this->error($validate->getError());
        }
        // 判断安装单状态
        if($data['sfwc'] == '2'){
            // 安装单状态
            $map['gczt'] = ['in','0,1,2,3,4'];
            $map['zzdpdzt'] = 3;
        } else {
            // 安装单状态 已完成
            $map['gczt'] = 5;
            $map['zzdpdzt'] = 3;
        }
        $selectdata = $this->request->param();
        $funt = new FunctionService;
        $selectazd = $funt->selectazd($selectdata);
        $map = array_merge($map,$selectazd);
        //分页
           $pege = input('param.pege');
           $limit = input('param.limit');
           $start = ($pege-1)*$limit;
        // 经销商编码
//        $map['jxs'] = $this->user['jxs_code'];
        // 安装队编码
//        $map['azd'] = $data['engineer_code'];
        // 设置字段
        $field = 'azd.engineer_name,hise.yh_name,hise.fgsfwfzr,hise.azd_id,hise.address,hise.provice,hise.city,hise.qx,hise.yyaz_sj,hise.jxsxsy,hise.hise_service_project_id,gczt';
        // 获取安装单列表信息
        $list = Db::table('cmf_rl_hise_service_project')
            // ->alias('hise')
            // ->join('cmf_rl_et_azd azd', 'hise.azd = azd.engineer_code')
            ->where($map)
            // ->field($field)
            ->order('hise_service_project_id desc')
            ->limit($start,$limit)
            ->select()->toArray();
        $provices = Db::name('rl_hise_area')->column('sf_name','sf_code');
        $citys = Db::name('rl_hise_dq_table')->column('dq_name','dq_code');
        $qxs = Db::name('rl_hise_qx_table')->column('qx_name','qx_code');
        // 遍历格式化数据等信息
        foreach ($list as $key => $val) {
            $list[$key]['yyaz_sj'] = $val['yyaz_sj'] ? date('Y-m-d', $val['yyaz_sj']) : '';
            $provice = $provices[$val['provice']];
            $city = $citys[$val['city']];
            $qx = $qxs[$val['qx']];
            $list[$key]['address'] = $provice . $city . $qx . $val['address'];
            $list[$key]['gczt_name'] = $this->install_status_new[$val['gczt']];
            $list[$key]['problem_num']  = Db::name('rl_inspect')->where('azd_id',$val['azd_id'])->where('is_problem',1)->where('inspect_status',2)->count();
            $list[$key]['engineer_name'] = Db::name('rl_et_azd')->where('engineer_code', $val['azd'])->value('engineer_name');
            unset($list[$key]['provice']);
            unset($list[$key]['city']);
            unset($list[$key]['qx']);
        }
        // 获取安装单状态数量
        $map['gczt'] = array('in','0,1,2,3,4');
        // 进行中
        $data['count_2'] = $this->installationMountCount($map);
        $map['gczt'] = 5;
        // 已完成
        $data['count_3'] = $this->installationMountCount($map);
        // 安装队列表
        $data['list'] = $list;
        // 数据返回
        $this->success('操作成功！', $data);
    }

    /**
     * 总部--安装单状态对应数量
     */
    protected function installationMountCount($map)
    {
        // 获取安装单数量
        $count = Db::table('cmf_rl_hise_service_project')
            // ->alias('hise')
            // ->join('cmf_rl_et_azd azd','hise.azd = azd.engineer_code')
            ->where($map)
            ->count();
        return $count;
    }

    //修改用户是否可以登录的状态
    public function islogin()
    {
        $mobilephone = $this->request->param('mobilphone');
        $status = $this->request->param('status');
        if (!$mobilephone || !in_array($status,[0,1,2])){
            $this->error('参数错误');
        }
        $data['status'] = $status;
        $data['update_time'] = time()+20;
        $res = Db::table('cmf_rl_zsms_user_query')->where('ignore',0)->where('mobilphone',$mobilephone)->update($data);
        if($res)
        {
            return json(['code'=>1,'msg'=>'修改成功']);
        }else{
            return json(['code'=>0,'msg'=>'修改失败']);
        }
        return json(['code'=>0,'msg'=>'修改失败']);
    }

 
}